﻿Imports HTLibrary
Imports TNCBAL
Imports TNCDAL

Public Class ucFileHBLGridView
    Private _objHBLController As New HBLController
    'Private _objFileMBL As tblFileMBL ' Thông tin File
    Private _IsExport As Boolean

    ''' <summary>
    ''' Gọi hàm khởi tạo này nếu muốn lấy toàn bộ các HBL
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New(ByVal isExport As Boolean)
        InitializeComponent()
        DataGridViewHelper.DataGridView_InitEvent(gridView)
        'Lưu thông tin FileMBL
        _IsExport = isExport
        BindingDataGridView(Nothing, -1)
        FormatDataGridView()

    End Sub

    ' ''' <summary>
    ' ''' Gọi hàm khởi tạo này nếu muốn lấy các HBL của một File
    ' ''' </summary>
    ' ''' <param name="obj"></param>
    ' ''' <remarks></remarks>
    'Public Sub New(ByVal obj As tblFileMBL)
    '    InitializeComponent()
    '    'Lưu thông tin File
    '    _objFileMBL = obj
    'End Sub

    Public ReadOnly Property DataGridView() As DevComponents.DotNetBar.Controls.DataGridViewX
        Get
            Return gridView
        End Get
    End Property

    Public Sub FormatDataGridView()
        Dim hideColumns As New List(Of String)({"Id_HBL"})
        If _IsExport Then
            hideColumns.Add("Amount")
            hideColumns.Add("VATPercent")
        End If

        Dim frozenColumns = Nothing
        Dim numberColumns As New List(Of String)({"GW", "NW", "CBM"})

        Dim renameColumns As New Dictionary(Of String, String)
        renameColumns.Add("TaxReturnNo", "Cus Sheet No")

        CViewer.FormatDataGridView(gridView, _
                                   hideColumns, _
                                   renameColumns, _
                                   frozenColumns)
        'Custome format number
        'Quantity column
        gridView.Columns("GW").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        gridView.Columns("GW").DefaultCellStyle.Format = "#,##0.000"

        gridView.Columns("NW").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        gridView.Columns("NW").DefaultCellStyle.Format = "#,##0.000"

        gridView.Columns("CBM").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        gridView.Columns("CBM").DefaultCellStyle.Format = "#,##0.0000"
    End Sub

    Public Sub BindingDataGridView(ByVal obj As tblFileMBL, Optional ByVal indexRow As Integer = -1)
        Dim query As IQueryable
        If obj IsNot Nothing Then
            query = _objHBLController.GetCustomHBLByFileMBL(obj.ID_FileMBL)
        Else
            query = _objHBLController.GetCustomHBLByFileMBL(-1)
        End If

        gridView.DataSource = query

        If indexRow > 0 Then
            'Set current row
            gridView.Rows(indexRow).Selected = True
            gridView.FirstDisplayedScrollingRowIndex = indexRow
        End If

        
    End Sub

    Public Sub BindingDataGridView(ByVal obj As tblFile)
        Dim query As IQueryable ' = _objHBLController.GetCustomHBLByFile(obj.ID_File)
        If obj IsNot Nothing Then
            query = _objHBLController.GetCustomHBLByFile(obj.ID_File)
        Else
            query = _objHBLController.GetCustomHBLByFile(-1)
        End If

        gridView.DataSource = query
    End Sub

    Private Sub ucFileHBLGridView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'BindDataGridView()
    End Sub

    Public dataGridViewXHBL_CurrentCellChangedEvent As EventHandler
    Private Sub dataGridViewXHBL_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gridView.CurrentCellChanged
        If dataGridViewXHBL_CurrentCellChangedEvent IsNot Nothing Then
            dataGridViewXHBL_CurrentCellChangedEvent(sender, e)
        End If
    End Sub

    Public dataGridViewXHBL_CellDoubleClickEvent As EventHandler
    Public Sub dataGridViewXHBL_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridView.CellDoubleClick
        If dataGridViewXHBL_CellDoubleClickEvent IsNot Nothing Then
            dataGridViewXHBL_CellDoubleClickEvent(sender, e)
        End If
    End Sub
End Class
